﻿//Author:Amitkuamr Hegde
//Email: amits.hegde@gmail.com
//Date: 10, April 2009
//Do whatever you wnat with the code :-)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace RegExTester
{
    public partial class frmRegExMatcher : Form
    {
        public frmRegExMatcher()
        {
            InitializeComponent();
        }

        private void txtTestText_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRegExpr.Text) && string.IsNullOrEmpty(txtTestText.Text))
            {
                lblRestult.Text = "Enter RegEx and Test String";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
                return;
            }else if(string.IsNullOrEmpty(txtRegExpr.Text)){
                lblRestult.Text = "Enter a Regular Expression for Matching.";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
                return;
            }

            if (!string.IsNullOrEmpty(txtTestText.Text))
            {
                PerformMatch();
            }
            else
            {
                lblRestult.Text = "Enter a Test string.";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
            }
        }

        private void txtRegExpr_TextChanged(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtRegExpr.Text) && string.IsNullOrEmpty(txtTestText.Text))
            {
                lblRestult.Text = "Enter RegEx and Test String";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
                return;
            }

            if (string.IsNullOrEmpty(txtRegExpr.Text))
            {
                lblRestult.Text = "Enter a Regular Expression for Matching.";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
                return;
            }
            if (!string.IsNullOrEmpty(txtTestText.Text))
            {
                PerformMatch();
            }
            else
            {
                lblRestult.Text = "Enter a Test string.";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
            }
        }

        private void PerformMatch()
        {
            try
            {
                Regex ex = new Regex(txtRegExpr.Text);

                if (ex.IsMatch(txtTestText.Text))
                {
                    lblRestult.Text = "Success :-)";
                    lblRestult.ForeColor = System.Drawing.Color.DarkGreen;
                }
                else
                {
                    lblRestult.Text = "Failed :-(";
                    lblRestult.ForeColor = System.Drawing.Color.DarkRed;
                }
            }
            catch (ArgumentException)
            {
                lblRestult.Text = "Enter a Valid Regular Expression.";
                lblRestult.ForeColor = System.Drawing.Color.DarkBlue;
            }
        }

        private void linkRegExReference_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore", @"http://www.regular-expressions.info/reference.html");
        }
    }
}
